﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MDF.Logger.Interfaces;
using System.IO;
using System.Diagnostics;

namespace MDF.Logger.Implements
{
    class FileLogger : ILog
    {

        #region Properties

        private string m_strPrefixFileName = "LogFile";
        private string m_strLogDir = "LogDir";//"~/LogDir/";

        private object LockObject = new object();

        private List<string> m_lstTemp = new List<string>();


        #endregion

        #region Emplement methods
        private void TryLog(StreamWriter strw)
        {
            foreach (var item in m_lstTemp)
            {
                strw.WriteLine(item);
            }
            m_lstTemp.Clear();
        }

        private void WriteLog(LOGTYPE type, string message, Exception ex)
        {
            var strem = GetLogFile();
            //if(type==LOGTYPE.ERROR)
            //    strem.WriteLine(this.GetDebugInfo());
            string strTemp = string.Format("[{0}] [{2}]: {1}", DateTime.Now.ToString("yyyy/MM/dd HH:mm"), message, type.ToString());
            if (strem == null)
            {
                m_lstTemp.Add(strTemp);
                return;
            }
            else
            {
                if (ex == null)
                {
                    strem.WriteLine(strTemp);
                }
                else
                {
                    strem.WriteLine(strTemp);
                    strTemp = string.Format("[{0}] [{2}]: {1}", DateTime.Now.ToString("yyyy/MM/dd HH:mm"), ex.Message, type.ToString());
                    strem.WriteLine(strTemp);
                    strTemp = string.Format("[{0}] [{2}]: {1}", DateTime.Now.ToString("yyyy/MM/dd HH:mm"), ex.StackTrace, type.ToString());
                    strem.WriteLine(strTemp);
                }
                strem.Close();
            }
        }

        private StreamWriter GetLogFile()
        {
            lock (LockObject)
            {
                try
                {
                    string strLogDirTemp = "";
                    if (System.Web.HttpContext.Current != null)
                    {
                        strLogDirTemp = System.Web.HttpContext.Current.Server.MapPath("~/" + m_strLogDir + "/");
                    }
                    if (string.IsNullOrEmpty(strLogDirTemp))
                    {
                        strLogDirTemp = AppDomain.CurrentDomain.BaseDirectory;
                        strLogDirTemp += string.Format("{0}\\", m_strLogDir);
                    }
                    if (!Directory.Exists(strLogDirTemp))
                        Directory.CreateDirectory(strLogDirTemp);
                    string strFile = strLogDirTemp + m_strPrefixFileName + "_" + DateTime.Now.ToString("yyyy_MM_dd") + ".txt";
                    if (!File.Exists(strFile))
                    {
                        return System.IO.File.CreateText(strFile);
                    }
                    return new StreamWriter(strFile, true);
                }
                catch
                {
                    return null;
                }
            }
        }

        public void Info(string str_Message)
        {
            this.Info(str_Message, null);
        }
        public void Info(string str_Message, Exception _exError)
        {
            WriteLog(LOGTYPE.INFO, str_Message, _exError);
        }
        public void Debug(string str_Message)
        {
            this.Debug(str_Message, null);
        }
        public void Debug(string str_Message, Exception _exError)
        {
            WriteLog(LOGTYPE.DEBUG, str_Message, _exError);
        }
        public void Warning(string str_Message)
        {
            this.Warning(str_Message, null);
        }
        public void Warning(string str_Message, Exception _exError)
        {
            WriteLog(LOGTYPE.WARINING, str_Message, _exError);
        }
        public void Error(string str_Message)
        {
            this.Error(str_Message, null);
        }
        public void Error(string str_Message, Exception _exError)
        {
            WriteLog(LOGTYPE.ERROR, str_Message, _exError);
        }

        public string[] GetALL()
        {
            try
            {
                string strLogDirTemp = "";
                if (System.Web.HttpContext.Current != null)
                {
                    strLogDirTemp = System.Web.HttpContext.Current.Server.MapPath("~/" + m_strLogDir + "/");
                }
                if (string.IsNullOrEmpty(strLogDirTemp))
                {
                    strLogDirTemp = AppDomain.CurrentDomain.BaseDirectory;
                    strLogDirTemp += string.Format("{0}\\", m_strLogDir);
                }
                if (!Directory.Exists(strLogDirTemp))
                    Directory.CreateDirectory(strLogDirTemp);
                return Directory.GetFileSystemEntries(strLogDirTemp);
            }
            catch
            {
                return null;
            }
        }

        public string[] GetDetail(string key)
        {
            string strLogDirTemp = "";
            if (System.Web.HttpContext.Current != null)
            {
                strLogDirTemp = System.Web.HttpContext.Current.Server.MapPath("~/" + m_strLogDir + "/");
            }
            if (string.IsNullOrEmpty(strLogDirTemp))
            {
                strLogDirTemp = AppDomain.CurrentDomain.BaseDirectory;
                strLogDirTemp += string.Format("{0}\\", m_strLogDir);
            }
            string strFile = strLogDirTemp + key + ".txt";
            if (File.Exists(strFile))
            {
                return System.IO.File.ReadAllLines(strFile);
            }
            return null;
        }

        #endregion

    }
}
